import axios from "axios";
import { Message } from "element-ui";
import { Modal } from "ant-design-vue";
import store from "@/store";
import { getToken, setToken, removeToken, removeUser } from "@/utils/auth";
import router, { resetRouter } from "@/router";
import qs from "querystring";

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 30000, // request timeout
});

const token = "";

// request interceptor
service.interceptors.request.use(
  (config) => {
    // do something before request is sent

    if (store.getters.token) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers["X-Token"] = getToken();
    }
    return config;
  },
  (error) => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    // res就是后端返回来的结果， { status,message,data,timestamp}
    const res = response.data;

    // if the custom code is not 20000, it is judged as an error.
    if (res.status === 200) {
      if (response.headers.refresh === "true") {
        refleshToken(response);
      }
      return res;
    } else if (res.status === 402 || res.status === 401) {
      logout();
    } else if (res.message === "后台接口异常！原因：null") {
      Message({
        message: "长时间未操作，请先刷新页面",
        type: "error",
        duration: 5 * 1000,
      });
      // 返回承诺失败对象
      return Promise.reject(new Error(res.message || "Error"));
    } else {
      // 消息弹框
      console.log(res.message);
      Message({
        message: res.message,
        type: "error",
        duration: 5 * 1000,
      });
      // 返回承诺失败对象
      return Promise.reject(new Error(res.message || "Error"));
    }
  },
  (error) => {
    console.log("err" + error); // for debug
    Message({
      message: error.message,
      type: "error",
      duration: 5 * 1000,
    });
    return Promise.reject(error);
  }
);
// 退出
let key = 0;
export function logout() {
  // 防止重复弹出
  key++;
  if (key == 1) {
    Modal.error({
      title: "登录已过期",
      content: "很抱歉，登录已过期，请重新登录",
      okText: "重新登录",
      mask: false,
      onOk: async () => {
        try {
          await store.dispatch("user/logout");
          removeToken();
          resetRouter();
          removeUser();
          router.push("/login");
          key = 0;
        } catch (e) {
          window.location.reload();
          key = 0;
        }
      },
    });
  }
}
async function refleshToken(response) {
  console.log(response);
  setToken(response.headers["x-token"]);
  // await getRefreshToken()
}
function getRefreshToken() {
  return new Promise((resolve, reject) => {
    axios
      .post(
        process.env.VUE_APP_BASE_API + "user/refreshToken",
        qs.stringify({ token: getToken() }),
        {
          headers: {
            "X-Token": getToken(),
          },
        }
      )
      .then((res) => {
        if (res.data.status == 200) {
          setToken(res.data.data.token);
          resolve(res);
        } else {
          logout();
        }
      })
      .catch((err) => {
        reject(err);
        logout();
      });
  });
}
export default service;
